package com.soilflow.web.controller.soilflow;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.dto.LandInfoDto;
import com.soilflow.common.domain.vo.LandInfoVo;
import com.soilflow.common.domain.vo.LandmarkVo;
import com.soilflow.common.domain.vo.TotalByTown;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.exception.ServiceException;
import com.soilflow.common.utils.NoUtils;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.service.LandClassifyService;
import com.soilflow.info.service.LandInfoService;
import com.soilflow.system.service.ISysDeptService;
import com.soilflow.web.utils.LandNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 土地信息Controller
 * 
 * @author relief
 * @date 2022-10-19
 */
@RestController
@RequestMapping("/land/info")
public class ReliefLandInfoController extends BaseController
{
    @Autowired
    private LandInfoService landInfoService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private LandClassifyService soilflowClassifyService;

    /**
     * 查询土地信息列表
     */
//    @PreAuthorize("@ss.hasPermi('land:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(LandInfoDto landInfo)
    {
        startPage();
        List<LandInfoVo> list = landInfoService.selectLandInfoList(landInfo);
        return getDataTable(list);
    }

    // 小程序这块使用,查询土地信息，用于更新土地的坐标和实景图片
    @GetMapping("/allList")
    public TableDataInfo allList(LandInfoDto landInfo)
    {
        startPage();
        List<LandInfoVo> list = landInfoService.selectAllLandList(landInfo);
        return getDataTable(list);
    }

    @GetMapping("/lists")
    public TableDataInfo lists(LandInfoDto landInfo)
    {
        startPage();
        List<LandInfoVo> list = landInfoService.lists(landInfo);
        return getDataTable(list);
    }

    @GetMapping("/listsJingweidu")
    public List<LandmarkVo> listsJingweidu()
    {
        List<LandmarkVo> list = landInfoService.listsJingweidu();
        return list;
    }

    @GetMapping("/parkJingweidu")
    public List<LandmarkVo> parkJingweidu()
    {
        List<LandmarkVo> list = landInfoService.parkJingweidu();
        return list;
    }

    @GetMapping("/searchLand")
    public AjaxResult searchLand(String landName) {
        return AjaxResult.success(landInfoService.searchLand(landName));
    }

    @GetMapping("/massifList")
    public TableDataInfo massifList(LandInfoDto landInfo) {
        startPage();
        List<LandInfoVo> list = landInfoService.massifList(landInfo);
        return getDataTable(list);
    }

    @GetMapping("/parkList")
    public TableDataInfo parkList(LandInfoDto landInfo) {
        startPage();
        List<LandInfoVo> list = landInfoService.parkList(landInfo);
        return getDataTable(list);
    }

    @GetMapping("/buildingList")
    public TableDataInfo buildingList(LandInfoDto landInfo) {
        startPage();
        List<LandInfoVo> list = landInfoService.buildingList(landInfo);
        return getDataTable(list);
    }

    /**
     * 查询土地信息下拉列表
     */
    @GetMapping("/all")
    public AjaxResult listAll(LandInfoDto landInfo) {
        landInfo.setPageSize(null);
        List<LandInfoVo> list = landInfoService.selectLandInfoList(landInfo);
        return AjaxResult.success(list);
    }

    /**
     * 获取土地信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('land:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(landInfoService.selectLandInfoById(id));
    }

    /**
     * 新增土地信息
     */
//    @PreAuthorize("@ss.hasPermi('land:info:add')")
    @Log(title = "土地信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LandInfoDto landInfo)
    {
        //校验土地信息
        LambdaQueryWrapper<LandInfo> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotEmpty(landInfo.getLandName()),LandInfo::getLandName,landInfo.getLandName())
                .last("limit 1");
        if (!ObjectUtils.isEmpty(landInfoService.getOne(query))){
            throw new ServiceException("土地名称已存在！");
        }
        // 必须位置需精确到村
        List<SysDept> sysDepts =  deptService.selectDeptListByAncestor(landInfo.getDeptId());
        if (sysDepts.size()>0){
            throw new ServiceException("土地所在地区需精确到村或社区！");
        }
        //设置土地编码
        LandNoUtils landNoUtils = new LandNoUtils();
        String No = landNoUtils.genNoUtil(deptService.selectDeptById(landInfo.getDeptId()));
        landInfo.setLandNo(No);
        // 新增土地并返回结果
        return toAjax(landInfoService.insertLandInfo(landInfo));
    }

    /**
     * 修改土地信息
     */
//    @PreAuthorize("@ss.hasPermi('land:info:edit')")
    @Log(title = "土地信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LandInfoDto landInfo)
    {
        return AjaxResult.success(landInfoService.updateLandInfo(landInfo));
    }

    @PostMapping("/position")
    public AjaxResult editPositon(@RequestBody LandInfoDto landInfo)
    {
        LandInfo param = new LandInfo();
        param.setId(landInfo.getId());
        param.setLongitude(landInfo.getLongitude());
        param.setLatitude(landInfo.getLatitude());
        return AjaxResult.success(landInfoService.updateById(param));
    }


    @PostMapping("/image")
    public AjaxResult editImage(@RequestBody LandInfoDto landInfo)
    {
        LandInfo param = new LandInfo();
        param.setId(landInfo.getId());
        param.setDetailImage(landInfo.getDetailImage());
        return AjaxResult.success(landInfoService.updateById(param));
    }

    /**
     * 删除土地信息
     */
//    @PreAuthorize("@ss.hasPermi('land:info:remove')")
    @Log(title = "土地信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable  List<Long> ids)
    {
        return toAjax(landInfoService.removeByIds(ids));
    }

    /**
     * 导出
     * @param response
     * @throws IOException
     */
    @PostMapping("export1")
    public void  exportExcel1(HttpServletResponse response) throws IOException {
        landInfoService.exportExcel1(response);
    }
    /**
     * 导入
     */
    @PostMapping("import")
    public void importExcel(MultipartFile file) throws IOException{
        landInfoService.importExcel(file,landInfoService,deptService,soilflowClassifyService);
    }
    /**
     * 按月对土地状态进行统计(这里是当前月往前推半年)
     */
    @GetMapping("/monthstatistics")
    public AjaxResult monthstatistics(){
        return AjaxResult.success(landInfoService.monthstatistics());
    }
    /**
     * 统计土地总数
     */
    @GetMapping("/totalLand")
    public AjaxResult totalLand(){
        return AjaxResult.success(landInfoService.totalLand());
    }
    /**
     * 统计总
     */
    @GetMapping("/totalstatistics")
    public AjaxResult statistics(){
        return AjaxResult.success(landInfoService.totalstatistics());
    }

    /**
     * 根据镇统计土地数量
     */
    @GetMapping("/totalByTown")
    public AjaxResult totalByTown() {
        List<TotalByTown> totalByTowns = landInfoService.totalByTown();
        return AjaxResult.success(totalByTowns);
    }



}